package com.yunji.web.cabinet;

import com.yunji.aggregate.model.dto.container.CabinetTemplateParams;
import com.yunji.aggregate.model.vo.container.CabinetTemplateVO;
import com.yunji.aggregate.service.ContainerAggregateService;
import com.yunji.cabinet.business.CabinetTemplateBusiness;
import com.yunji.cabinet.domain.CabinetTemplateDO;
import com.yunji.cabinet.dto.TemplateDTO;
import com.yunji.common.annotation.Log;
import com.yunji.common.base.BaseController;
import com.yunji.common.base.response.Response;
import com.yunji.common.base.response.ResponseHelper;
import com.yunji.common.enums.common.BusinessType;
import com.yunji.common.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Package com.yunji.web.cabinet
 * FileName:	CabinetTemplateController.java
 * Copyright:	Copyright © 2024 云集互联 All Rights Reserved
 * Company:		云集互联
 * @version: 1.0
 * @since: JDK 1.8.0_202
 * Create at:   2024-04-07 16:35:08
 * Description:
 * 商品模板交互
 * Modification History:
 * Author					Date						Version			Description
 * --------------------------------------------------------------------------------
 * HuaAo				2024-04-07 16:35:08			1.0				1.0 Version
 */
@RestController
@Api(tags = "管理端-商品模板信息")
@RequestMapping("/container/template")
public class CabinetTemplateController extends BaseController {
    @Resource
    private CabinetTemplateBusiness templateBusiness;

    @Resource
    private ContainerAggregateService aggregateService;

    @ApiOperation("查询模板管理列表")
    @PreAuthorize("@ss.hasPermi('container:template:query')")
    @GetMapping("/list")
    public Response<List<CabinetTemplateVO>> list(@Valid CabinetTemplateParams template) {
        List<CabinetTemplateVO> list = aggregateService.searchTemplateVOList(template);
        return ResponseHelper.successWithPage(list);
    }

    @ApiOperation("导出模板管理列表")
    @Log(title = "模板管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('container:template:export')")
    @PostMapping("/export")
    public void export(CabinetTemplateParams template) {
        List<CabinetTemplateVO> list = aggregateService.searchTemplateVOList(template);
        ExcelUtil<CabinetTemplateVO> util = new ExcelUtil<>(CabinetTemplateVO.class);
        util.exportExcel(response, list, "模板管理数据");
    }

    @ApiOperation("获取模板管理详细信息")
    @GetMapping(value = "/{id}")
    public Response<CabinetTemplateVO> getInfo(@PathVariable("id") Long id) {
        return ResponseHelper.success(aggregateService.selectCabinetTemplate(id));
    }

    @ApiOperation("新增模板管理")
    @Log(title = "模板管理", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('container:template:add')")
    @PostMapping
    public Response<Long> add(@RequestBody CabinetTemplateDO template) {
        return ResponseHelper.success(templateBusiness.create(template));
    }

    @ApiOperation("修改模板管理")
    @Log(title = "模板管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('container:template:edit')")
    @PutMapping
    public Response<String> update(@RequestBody TemplateDTO template) {
        Assert.isTrue(templateBusiness.update(template), "编辑失败");
        return ResponseHelper.success("编辑成功");
    }

    @ApiOperation("删除模板管理")
    @Log(title = "模板管理", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('container:template:remove')")
    @DeleteMapping("/{ids}")
    public Response<String> remove(@PathVariable Long... ids) {
        templateBusiness.delete(ids);
        return ResponseHelper.success("删除成功");
    }


    /**
     * ------------------------------------------搜索栏------------------------------------------
     */

    @ApiOperation("创建人名称匹配搜索")
    @GetMapping("/searchCreateName")
    public Response<List<String>> searchCreateName(@RequestParam("createName") String createName) {
        List<String> list = templateBusiness.searchCreateName(createName);
        return ResponseHelper.successWithPage(list);
    }
}
